package com.baoming.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baoming.dao.IArticleDao;
import com.baoming.domain.Article;
import com.baoming.service.IArticleService;
import com.baoming.util.PageInfo;

@Transactional(readOnly=true)
@Service(IArticleService.SERVICE)
public class ArticleServiceImpl implements IArticleService {
	
	@Resource(name=IArticleDao.SERVICE)
	private IArticleDao articleDao ;

	/**
	 * 保存文章 
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveArticle(Article article) {
		articleDao.save(article) ; 
	}

	/**
	 * 分页查询文章 
	 */
	public List<Article> selectArticleWithPage(PageInfo pageInfo , String memberId) {
		String hql = "and m.isDelete = 1 and m.memberId = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<Article> articles = articleDao.findCollectionByConditionWithPage(hql, params.toArray(), orderby, pageInfo) ; 
		return articles ;
	}

	/** 
	 * 按顺序查询出个人文章 
	 * (non-Javadoc)
	 * @see com.baoming.service.IArticleService#selectArticleWithSort(com.baoming.util.PageInfo, java.lang.String)
	 */
	public List<Article> selectArticleWithSort(PageInfo pageInfo,
			String memberId) {
		String hql = " and m.isShow = 1 and m.isDelete = 1 and m.memberId = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>() ; 
		orderby.put("m.sort", "desc") ; 
		List<Article> articles = articleDao.findCollectionByConditionWithPage(hql, params.toArray(), orderby, pageInfo) ; 
		return articles ;
	}

	public Article selectArticle(String articleId) {
		return articleDao.findObjectById(articleId);
	}

	@Transactional(readOnly=false)
	public void deleteArticle(String id) {
		articleDao.deleteObjectById(id) ; 
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void modifyArticle(Article oldArticle) {
		articleDao.update(oldArticle) ; 
	}

	/**
	 * 文章分页搜索 
	 */
	public List<Article> selectSearchArticleWithPage(String keyword,
			PageInfo pageInfo, String memberId) {
		String hql = "and m.isDelete = 1 and m.memberId = ? and m.title like ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(memberId) ; 
		params.add(keyword+"%") ; 
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>() ; 
		orderby.put("m.addTime", "desc") ; 
		List<Article> articles = articleDao.findCollectionByConditionWithPage(hql, params.toArray(), orderby, pageInfo) ; 
		return articles ;
	} 
	
}
